<?php
Yii::import('application.components.dao.class.Dao');
class AuctionResourceDao extends Dao{
    protected $_resources = array();
    public $auction_id;
    const RESOURCE_TYPE_IMAGE = 'IMAGES';
    
    protected $_resourceMapper = array(
        self::RESOURCE_TYPE_IMAGE => 'ResourceImg'
    );
    
    public function setImages($resources){
        return $this->_resources[self::RESOURCE_TYPE_IMAGE] = $resources;
    }
      
    public function getImages(){
        $auctionResource = AuctionResource::model()->with('images')->findAllByAttributes(array('auction_id' => $this->auction_id,'resource_type' => self::RESOURCE_TYPE_IMAGE));        
        $images = array();
        if (!empty($auctionResource)){
            foreach ($auctionResource as $resourceData){
                $images[$resourceData->resource_key] = $resourceData->images->path;
            }
        }        
        return $images;
    }
    
    public function findByPk($id = null){
        $this->auction_id = $id;
        return $this;
    }
    
    public function findByAttributes($attributes){
        
        return $this;
    }
    
    public function save($runValidation=true,$attributes=null){
        if(!$runValidation || $this->validate($attributes)){
            if (!empty($this->_resources)){

                $this->saveImage();
                
            }
        }else{
            return false;
        }
    }
    
    public function saveImage(){
        $images = $this->getImages();
        $save = array();
        foreach ($this->_resources[self::RESOURCE_TYPE_IMAGE] as $resourceDataKey => $resource){
            if (array_key_exists($resourceDataKey, $images)){
                if (!empty($images[$resourceDataKey]) && !empty($this->_resources[self::RESOURCE_TYPE_IMAGE][$resourceDataKey]['path'])){
                   if ($images[$resourceDataKey] != $this->_resources[self::RESOURCE_TYPE_IMAGE][$resourceDataKey]['path']){
                       $save['update'][$resourceDataKey] = $this->_resources[self::RESOURCE_TYPE_IMAGE][$resourceDataKey];
                   }
                }
                if (empty($images[$resourceDataKey]) && !empty($this->_resources[self::RESOURCE_TYPE_IMAGE][$resourceDataKey]['path'])){
                    $save['update'][$resourceDataKey] = $this->_resources[self::RESOURCE_TYPE_IMAGE][$resourceDataKey];
                }
                /*
                if (empty($images[$resourceDataKey]) && empty($this->_resources[self::RESOURCE_TYPE_IMAGE][$resourceDataKey]['path'])){
                    
                }
                */
            }else{
                if (!empty($this->_resources[self::RESOURCE_TYPE_IMAGE][$resourceDataKey]['path'])){
                    $save['insert'][$resourceDataKey] = $this->_resources[self::RESOURCE_TYPE_IMAGE][$resourceDataKey];
                }
                
            }
        }
        
        
        if (array_key_exists('update', $save)){
            foreach ($save['update'] as $saveKey => $saveData){
                $auctionResource = new AuctionResource();
                $auctionResource->updateAll(array('resource_id' => $this->getResourceImage($saveData)),'resource_key = "'.$saveKey.'" AND auction_id = '.$this->auction_id);
                
            }
        }
        if (array_key_exists('insert', $save)){
            foreach ($save['insert'] as $saveKey => $saveData){
                $auctionResource = new AuctionResource();
                $auctionResource->resource_type = self::RESOURCE_TYPE_IMAGE;
                $auctionResource->resource_id   = $this->getResourceImage($saveData);
                $auctionResource->auction_id    = $this->auction_id;
                $auctionResource->resource_key  = $saveKey;
                $auctionResource->save();
            }
        }
        
    }
    
    public function getResourceImage($resource){
        $resourceAr = new ResourceImg();
        return $resourceAr->buildResource($resource);
    }
    
}